package com.github.blanexie.tserver.file;

import cn.hutool.core.util.StrUtil;

import java.util.HashMap;
import java.util.Map;

public class MimeTypeFinder {
    private static final Map<String, String> mimeTypeMap = new HashMap<>();

    static {
        //默认作为二进制文件返回
        mimeTypeMap.put("default", "application/octet-stream");

        // 文本文件
        mimeTypeMap.put("txt", "text/plain");
        mimeTypeMap.put("csv", "text/csv");
        mimeTypeMap.put("html", "text/html");
        mimeTypeMap.put("htm", "text/html");
        mimeTypeMap.put("xml", "text/xml");
        mimeTypeMap.put("json", "application/json");
        mimeTypeMap.put("js", "application/javascript");
        mimeTypeMap.put("css", "text/css");
        mimeTypeMap.put("sql", "text/x-sql");

        // 办公文档
        mimeTypeMap.put("doc", "application/msword");
        mimeTypeMap.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        mimeTypeMap.put("xls", "application/vnd.ms-excel");
        mimeTypeMap.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        mimeTypeMap.put("ppt", "application/vnd.ms-powerpoint");
        mimeTypeMap.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        mimeTypeMap.put("pdf", "application/pdf");
        mimeTypeMap.put("rtf", "application/rtf");

        // 图像文件
        mimeTypeMap.put("jpg", "image/jpeg");
        mimeTypeMap.put("jpeg", "image/jpeg");
        mimeTypeMap.put("png", "image/png");
        mimeTypeMap.put("gif", "image/gif");
        mimeTypeMap.put("bmp", "image/bmp");
        mimeTypeMap.put("tiff", "image/tiff");
        mimeTypeMap.put("webp", "image/webp");
        mimeTypeMap.put("svg", "image/svg+xml");

        // 音频文件
        mimeTypeMap.put("mp3", "audio/mpeg");
        mimeTypeMap.put("wav", "audio/x-wav");
        mimeTypeMap.put("ogg", "audio/ogg");

        // 视频文件
        mimeTypeMap.put("mp4", "video/mp4");
        mimeTypeMap.put("webm", "video/webm");
        mimeTypeMap.put("ogv", "video/ogg");
        mimeTypeMap.put("avi", "video/x-msvideo");
        mimeTypeMap.put("wmv", "video/x-ms-wmv");
        mimeTypeMap.put("flv", "video/x-flv");
        mimeTypeMap.put("mov", "video/quicktime");
        mimeTypeMap.put("mkv", "video/x-matroska");
        mimeTypeMap.put("3gp", "video/3gpp");
        mimeTypeMap.put("m4v", "video/mp4"); // M4V 通常是 MP4 容器的视频

        // 压缩文件
        mimeTypeMap.put("zip", "application/zip");
        mimeTypeMap.put("rar", "application/x-rar-compressed");
        mimeTypeMap.put("tar", "application/x-tar");
        mimeTypeMap.put("gz", "application/gzip");

        // 其他常用类型
        mimeTypeMap.put("ico", "image/x-icon");
        mimeTypeMap.put("woff", "application/font-woff");
        mimeTypeMap.put("woff2", "application/font-woff2");
        mimeTypeMap.put("eot", "application/vnd.ms-fontobject");
        mimeTypeMap.put("ttf", "application/x-font-ttf");
        mimeTypeMap.put("otf", "application/x-font-otf");
        mimeTypeMap.put("swf", "application/x-shockwave-flash");

        //编程语言文件
        mimeTypeMap.put("java", "text/x-java");
        mimeTypeMap.put("kt", "text/x-kotlin");
        mimeTypeMap.put("js", "application/javascript");
        mimeTypeMap.put("ts", "application/typescript");
        mimeTypeMap.put("py", "text/x-python");
        mimeTypeMap.put("rb", "text/x-ruby");
        mimeTypeMap.put("php", "application/x-httpd-php");
        mimeTypeMap.put("c", "text/x-c");
        mimeTypeMap.put("cpp", "text/x-c++src");
        mimeTypeMap.put("cs", "text/x-csharp");
        mimeTypeMap.put("go", "text/x-go");
        mimeTypeMap.put("swift", "application/x-swift");
        mimeTypeMap.put("rs", "text/x-rustsrc");
        mimeTypeMap.put("scala", "text/x-scala");
        mimeTypeMap.put("groovy", "text/x-groovy");
        mimeTypeMap.put("clj", "text/x-clojure");
        mimeTypeMap.put("hs", "text/x-haskell");
        mimeTypeMap.put("erl", "text/x-erlang");
        mimeTypeMap.put("el", "application/emacs-lisp");

    }

    /**
     * 根据文件名获取MIME类型。
     *
     * @param fileName 文件名（包含扩展名）
     * @return MIME类型，如果找不到匹配项则返回null
     */
    public static String getMimeType(String fileName) {
        String defaultContentType = mimeTypeMap.get("default");
        if (fileName == null) {
            return defaultContentType;
        }

        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) {
            return defaultContentType;   // 没有找到扩展名
        }

        String extension = fileName.substring(dotIndex + 1).toLowerCase();
        String contentType = mimeTypeMap.get(extension);
        if (StrUtil.isEmpty(contentType)) {
            return defaultContentType;
        } else {
            return contentType;
        }
    }


}